/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.component.spreadsheet.analyze;

import cn.ac.nercis.pes.community.component.spreadsheet.TableSheetView;
import cn.ac.nercis.pes.model.dto.evaluation.onsite.SimpleEvalRecordDTO;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import org.controlsfx.control.spreadsheet.GridBase;
import org.controlsfx.control.spreadsheet.SpreadsheetCell;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;

/**
 * 单元指标测评记录查看表控件
 * @author zpy
 * @version 1.0.0
 */
public class UnitRecordSheetView extends TableSheetView<SimpleEvalRecordDTO> {
    private final static Integer EMPTY_DATA_COUNT = 1;
    private final static List<String> COLUMNS = List.of("测评对象","测评结论","结果记录");
    private final static Integer NAME_COLUMN_INDEX = 0;
    private final static Integer CONCLUSION_COLUMN_INDEX = 1;
    private final static Integer CONTENT_COLUMN_INDEX = 2;

    public UnitRecordSheetView(){
        this.getStyleClass().add("pes-sheet-view");

        var grid = new GridBase(EMPTY_DATA_COUNT, COLUMNS.size());
        grid.setRowHeightCallback(new GridBase.MapBasedRowHeightFactory(heardRowHeight()));
        grid.getColumnHeaders().addAll(COLUMNS);
        setGrid(grid);
        getColumns().get(NAME_COLUMN_INDEX).setPrefWidth(235);
        getColumns().get(CONCLUSION_COLUMN_INDEX).setPrefWidth(100);
        getColumns().get(CONCLUSION_COLUMN_INDEX).setMinWidth(100);
        getColumns().get(CONTENT_COLUMN_INDEX).setPrefWidth(400);
        this.editModel.set(false);
    }

    public void loadContent(List<SimpleEvalRecordDTO> data){
        this.sheetData= data;
        this.clearContent();
        for(var dataIndex = 0; dataIndex< data.size(); dataIndex++){
            this.loadContent(data.get(dataIndex),toSheetRowIndex(dataIndex));
        }
        setGrid(getGrid());
    }

    @Override
    protected void loadContent(SimpleEvalRecordDTO info,Integer sheetRowIndex){
        final ObservableList<SpreadsheetCell> data = FXCollections.observableArrayList();

        data.add(getTextAreaCell(sheetRowIndex,NAME_COLUMN_INDEX,info.getAssetName(),false));
        data.add(getTextFieldCell(sheetRowIndex,CONCLUSION_COLUMN_INDEX,info.getConclusion(),false));
        data.add(getTextAreaCell(sheetRowIndex,CONTENT_COLUMN_INDEX,processNewline(info.getContent()),false));

        this.getGrid().getRows().add(data);
    }

    @Override
    protected void clearContent(){
        int endIndex = this.getGrid().getRows().size() -1;
        for(;endIndex>=0;endIndex--){
            this.getGrid().getRows().remove(endIndex);
        }
    }

    @Override
    protected boolean isEmptyData(Integer sheetRowIndex) {
        return toDataIndex(sheetRowIndex) < 0;
    }

    @Override
    protected boolean validate(ObservableList<SpreadsheetCell> rowData) {
        return true;
    }

    @Override
    protected void clearValidate(ObservableList<SpreadsheetCell> rowData) {
    }

    @Override
    protected List<String> getDataColumns() {
        return COLUMNS;
    }

    @Override
    protected ObservableList<SpreadsheetCell> generateEmptyDataRow(int rowIndex) {
        return null;
    }

    @Override
    protected void deleteData() {
    }

    protected Map<Integer,Double> heardRowHeight(){
        Map<Integer, Double> rowHeight = new HashMap<>(1);
        IntStream.range(0,20).forEach(number->rowHeight.put(number,80.0));
        return rowHeight;
    }

}